<template>
  <div>
    <el-drawer
      :append-to-body="true"
      :show-close="false"
      :close-on-press-escape="false"
      size="1000px"
      title="选择历史排课"
      :visible="show"
      @close="handleClose"
    >
      <div class="compontent_content" v-loading="loading">
        <div class="name">{{ semesterName }}</div>
        <div class="sign">注：仅展示其他学期未结束课程，选择后将添加在本学期排课中继续上课</div>
        <div>
          <el-select
            filterable
            class="param_ipt"
            placeholder="请选择历史学期"
            v-model="semesterId"
          >
            <el-option
              v-for="item in semesterList"
              :key="item.semesterId"
              :label="item.semesterName"
              :value="item.semesterId"
            >
            </el-option>
          </el-select>
        </div>
        <div class="content_body" v-if="courseSchedulingList.length > 0">
          <transition-group name="list" tag="div">
            <div
              class="total_item"
              v-for="(item) in courseSchedulingList"
              :key="item.courseId"
            >
              <div class="course_name">
                <img
                  class="course_icon"
                  src="@/assets/images/group_icon/course_icon.png"
                  alt="加载失败"
                />《{{ item.courseName }}》
              </div>
              <div class="scheduling_total_item">
                <div
                  class="scheduling_item"
                  v-for="schedulingItem in item.schedulingList"
                  :key="schedulingItem.id"
                >
                  <div class="class_name">
                    <img
                      class="class_icon"
                      src="@/assets/images/group_icon/class_icon.png"
                      alt="加载失败"
                    />
                    <div class="w_160">
                      <tooltip :text="schedulingItem.schedulingName" width="100%"></tooltip>
                    </div>
                  </div>
                  <div class="student_count">
                    <img
                      class="all_person_icon"
                      src="@/assets/images/group_icon/all_person_icon.png"
                      alt="加载失败"
                    />
                    整班 <span>{{ schedulingItem.studentCount }}</span> 人
                  </div>
                  <div class="student_count">
                    <img
                      class="all_person_icon"
                      src="@/assets/images/group_icon/progress_icon.png"
                      alt="加载失败"
                    />
                    整班进度<span>{{ schedulingItem.learningProgress ? schedulingItem.learningProgress.toFixed(2) : 0 }}</span>%
                  </div>
                  <div class="student_count">
                    授课老师
                    <div class="w_140 teacher_name">
                      <tooltip :text="schedulingItem.teacherNames" width="100%"></tooltip>
                    </div>
                  </div>
                  <div class="absolute">
                    <el-checkbox v-model="schedulingItem.checked"></el-checkbox>
                  </div>
                </div>
                <div class="empty" v-if="item.schedulingList.length == 0">
                  <img src="@/assets/images/group_icon/course_empty_icon.png" alt="加载失败"/>
                  <div class="empty_text">
                    暂无排课
                  </div>
                </div>
              </div>
            </div>
          </transition-group>
        </div>
        <div class="content_body" v-if="courseSchedulingList.length == 0">
          <div class="empty">
            <img src="@/assets/images/group_icon/course_empty_icon.png" alt="加载失败"/>
            <div class="empty_text">
              暂无课程
            </div>
          </div>
        </div>
        <div class="btns">
          <el-button class="btn" @click="handleClose">取消</el-button>
          <el-button :disabled="!addNum" class="btn" type="primary" @click="submitForm">确定 <span v-if="addNum">({{ addNum }})</span>
          </el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { getHistoryList, getHistorySchedulingList, addSemesterByHistory } from '@/api/teach/teach'

export default {
  name: 'historyTeachingDrawer',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    port: {
      type: String,
      default: ''
    },
    schoolSemesterId: {
      type: [String, Number],
      default: ''
    },
    courseId: {
      type: [String, Number],
      default: ''
    },
    semesterName: {
      type: [String],
      default: '--'
    }
  },
  data() {
    return {
      loading: false,
      semesterId: '',
      courseSchedulingIds: [],
      addNum: '',
      addText: '',
      semesterList: [],
      courseSchedulingList: []
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.getHistoryList()
      }
    },
    semesterId: {
      handler() {
        this.$nextTick(() => {
          if (this.show) {
            this.getHistorySchedulingList()
          }
        })
      },
      immediate: true
    },
    courseSchedulingList: {
      handler(val) {
        this.$nextTick(() => {
          let num = 0
          let text = ''
          let courseSchedulingIds = []
          for (const courseSchedulingListElement of val) {
            if (courseSchedulingListElement.schedulingList.length > 0) {
              for (const schedulingListElement of courseSchedulingListElement.schedulingList) {
                if (schedulingListElement.checked) {
                  if (!text.includes(courseSchedulingListElement.courseName)) {
                    text += `《${courseSchedulingListElement.courseName}》`
                  }
                  text += `${schedulingListElement.schedulingName};`
                  num++
                  courseSchedulingIds.push(schedulingListElement.courseSchedulingId)
                }
              }
            }
          }
          this.addText = text.slice(0, text.length - 1)
          this.addNum = num
          this.courseSchedulingIds = courseSchedulingIds
        })
      },
      deep: true
    }
  },
  methods: {
    submitForm() {
      if (!this.addNum) {
        this.$modal.msgWarning('请选择要添加的排课')
        return
      }
      this.$confirm(`<div>是否确认在${this.semesterName}添加以下排课：</div>${this.addText}`, '添加确认', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const loading = this.$loading({
          lock: true,
          text: `正在处理中`,
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.3)'
        })
        try {
          let params = {
            semesterId: this.schoolSemesterId,
            courseSchedulingIds: this.courseSchedulingIds.join(',')
          }
          let res = await addSemesterByHistory(params)
          const { code } = res
          if (code == 200) {
            loading.close()
            this.$modal.msgSuccess('添加成功')
            this.handleClose()
            this.$emit('refresh', true)
          }
        } catch (e) {
          console.error(e)
        } finally {
          loading.close()
        }
      }).catch(() => {
        this.$modal.msgInfo('已取消')
      })
    },
    handleClose() {
      this.semesterId = ''
      this.courseSchedulingList = []
      this.$emit('update:show', false)
    },
    async getHistoryList() {
      this.loading = true
      try {
        let params = {
          semesterId: this.schoolSemesterId
        }
        let res = await getHistoryList(params)
        const { code, data } = res
        if (code == 200) {
          this.semesterList = data
          this.semesterId = data[0].semesterId
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },
    async getHistorySchedulingList() {
      try {
        let params = {
          historySemesterId: this.semesterId
        }
        if (this.courseId) {
          params.courseId = this.courseId
        }
        if (this.port == 'teacher') {
          params.userId = this.$store.getters.userId
        }
        let res = await getHistorySchedulingList(this.semesterId, params)
        const { code, data } = res
        if (code == 200) {
          for (const datum of data) {
            if (datum.schedulingList.length > 0) {
              for (const schedulingListElement of datum.schedulingList) {
                schedulingListElement.checked = false
              }
            }
          }
          this.courseSchedulingList = data
        }
      } catch (e) {
        console.error(e)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.compontent_content {
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;

  .name {
    font-weight: bold;
    color: #1ab394;
    font-size: 16px;
  }

  .sign {
    color: #ff9724;
    font-size: 16px;
    margin: 10px 0;
  }

  .content_body {
    margin-top: 20px;
    width: 100%;
    height: calc(100vh - 260px);
    overflow: auto;

    .total_item {
      margin-top: 20px;

      .course_name {
        font-size: 18px;
        font-family: PingFangSC, PingFang SC, sans-serif;
        font-weight: 400;
        color: #333333;
        line-height: 25px;
        display: flex;
        align-items: center;

        .course_icon {
          user-select: none;
          width: 24px;
          height: 24px;
          margin-right: 10px;
        }
      }

      .scheduling_total_item {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        margin-top: 10px;
      }

      .scheduling_item {
        width: 256px;
        height: 160px;
        background: #ffffff;
        border-radius: 12px;
        border: 1px solid #ebebeb;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 16px;
        box-sizing: border-box;
        position: relative;
        margin: 0 0 10px 16px;
        cursor: pointer;
        user-select: none;

        .absolute {
          position: absolute;
          top: 10px;
          right: 10px;
          align-items: center;
        }

        .class_name {
          font-size: 16px;
          font-family: PingFangSC, PingFang SC, sans-serif;
          font-weight: 600;
          color: #333333;
          line-height: 18px;
          display: flex;
          align-items: center;

          .class_icon {
            width: 24px;
            height: 24px;
            margin-right: 8px;
          }
        }

        .student_count {
          font-size: 14px;
          font-family: PingFangSC, PingFang SC, sans-serif;
          font-weight: 400;
          color: #99a1b3;
          line-height: 22px;
          margin-top: 10px;
          display: flex;
          align-items: center;

          .all_person_icon {
            width: 16px;
            height: 16px;
            margin-right: 8px;
          }

          span {
            font-size: 16px;
            font-family: PingFangSC, PingFang SC, sans-serif;
            font-weight: 600;
            color: #333333;
            margin: 0 4px;
          }

          .teacher_name {
            font-size: 16px;
            font-family: PingFangSC, PingFang SC, sans-serif;
            font-weight: 600;
            color: #333333;
            margin: 0 4px;
          }
        }
      }

      .scheduling_item:hover {
        border: 0;
        box-shadow: 0 0 20px 0 rgba(0, 55, 50, 0.12);
      }
    }

    .total_item:first-child {
      margin-top: 0;
    }
  }

  .btns {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: flex-end;
    gap: 0 10px;

    .btn {
      width: 90px;
    }
  }
}

.empty {
  padding-top: 20px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;

  .empty_text {
    user-select: none;
    margin-top: 20px;
    font-size: 18px;
    font-family: PingFangSC, PingFang SC, sans-serif;
    font-weight: 400;
    color: #666666;
  }
}

.w_160 {
  width: 160px;
}

.w_140 {
  width: 140px;
}
</style>
